<template>
  <div class="storeUser common-container">
    <div class="common-top">
      <span class="left">
        <!-- <el-form
          :inline="true"
          :model="formInline"
          size="small"
          class="demo-form-inline"
        >
          <el-form-item label="搜索">
            <el-input
              v-model="formInline.searchVal"
              placeholder="搜索"
            ></el-input>
          </el-form-item>
        </el-form> -->
      </span>
      <el-button type="primary" @click="toModify('add')">添加</el-button>
    </div>
    <div class="common-bot">
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column label="序号" type="index" width="80">
        </el-table-column>
        <el-table-column prop="address" label="头像">
          <template slot-scope="scope">
            <el-image style="width: 100px;" :size="'small'" :src="scope.row.cover"></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="title" label="名称"></el-table-column>
        <el-table-column prop="content" label="标签">
          <template slot-scope="scope">
            <span>{{scope.row.tags?scope.row.tags.join(","):"--"}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="role" label="类型">
          <template slot-scope="scope">
            <span>{{ typeFormat[scope.row.type] }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="city" label="操作">
          <template slot-scope="scope">
            <span
              class="span-modify edit"
              @click.stop="doModify('edit', scope.row)"
              >编辑</span
            >
            <span
              class="span-modify del"
              @click.stop="doModify('del', scope.row)"
              >删除</span
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="nowPage"
          :page-size="limit"
          layout="total, prev, pager, next"
          :total="totalElement"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import ManaApi from "@/api/index";
import { CodeToText } from "element-china-area-data";

export default {
  name: "storeUser",
  data() {
    return {
      formInline: {
        searchVal: "",
      },
      tableData: [],
      typeFormat:{},
      nowPage: 1,
      limit: 10,
      totalElement: 0,
      genderFormat: {
        male: "男",
        female: "女",
      },
      CodeToText,
    };
  },
  watch: {
    "formInline.searchVal"(val) {
      this.getData(val);
    },
  },
  methods: {
    handleSizeChange(val) {
      this.limit = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.nowPage = val;
      this.getData();
    },
    doModify(type, item) {
      if (type == "edit") {
        this.$router.push({
          name: "newsModify",
          query: {
            type,
            id: item.id,
          },
        });
      } else if (type == "del") {
        this.$confirm("确认此操作?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          ManaApi.news
            .del({
              targetIds: [item.id],
            })
            .then((res) => {
              if (res.code == 200) {
                this.$message.success("操作成功");
                this.getData();
              }
            });
        });
      }
    },
    toModify(type) {
      this.$router.push({
        name: "newsModify",
        query: {
          type,
        },
      });
    },
    getData(keyword = "") {
      ManaApi.news
        .list({
          limit: this.limit,
          page: this.nowPage - 1,
          keyword,
        })
        .then((res) => {
          this.tableData = res.data;
          this.totalElement = res.total;
        });
    },
    getConstant() {
      return new Promise((resolve, reject) => {
        ManaApi.constant
          .all({})
          .then((res) => {
            let typeFormat = {};
            res.data.NewsType.forEach(item=>{
              typeFormat[item.value] = item.text;
            })
            this.typeFormat = typeFormat;
            resolve();
          })
          .catch((err) => {
            reject();
          });
      });
    },
  },
  async mounted() {
    await this.getConstant();
    this.getData();
  },
};
</script>
<style lang="scss" scoped>
.storeUser {
}
</style>